package apis

import (
	"github.com/gin-gonic/gin"
	"common/logger"
	"net/http"
	"tylnmall/constant"
	"time"
	"tylnmall/models"
	"github.com/jinzhu/gorm"
	"strings"
	"fmt"
)

func AddBackApi(c *gin.Context) {
	type param struct {
		PostId int    `json:"post_id" form:"post_id"` //帖子id
		UserId int    `json:"user_id" form:"user_id"` //用户id
		Mes    string `json:"mes" form:"mes"`         //正文
	}
	var p param
	var err error
	var back models.Backpost
	if err = c.Bind(&p); err != nil {
		logger.Error(err)
		c.JSON(http.StatusOK, gin.H{"err_code": constant.ErrorParamWrong, "err_msg": constant.TranslateErrCode(constant.ErrorParamWrong)})
		return
	}
	if p.PostId == 0 || p.UserId == 0 || p.Mes == "" {
		logger.Error(err)
		c.JSON(http.StatusOK, gin.H{"err_code": constant.ErrorParamWrong, "err_msg": constant.TranslateErrCode(constant.ErrorParamWrong)})
		return
	}
	const base_format = "2006-01-02 15:04:05"
	//获取当前时间
	nt := time.Now()
	var timeStr = nt.Format(base_format)
	back.Createtime = timeStr
	back.UserId = p.UserId
	back.Message = p.Mes
	back.PostId = p.PostId
	db := c.MustGet("ty").(*gorm.DB)
	if err = db.Create(&back).Error; err != nil {
		logger.Error(err)
		c.JSON(http.StatusOK, gin.H{"err_code": constant.ErrorDB, "err_msg": constant.TranslateErrCode(constant.ErrorDB)})
		return
	}
	type posts struct {
		Id           int    `json:"id"`
		Back         int    `json:"back"`
		Lastbacktime string `json:"lastbacktime" form:"Lastbacktime"` //最后回复时间
		Createtime   string `json:"createtime" form:"createtime"`     //创建时间
	}
	var post posts
	var Post models.Post
	if err = db.Where("id = ?", p.PostId).Find(&Post).Error; err != nil {
		logger.Error(err)
		c.JSON(http.StatusOK, gin.H{"err_code": constant.ErrorDB, "err_msg": constant.TranslateErrCode(constant.ErrorDB)})
		return
	}
	post.Id = Post.Id
	post.Back = Post.Back + 1
	fmt.Println()
	dates := strings.Split(strings.Split(Post.Createtime, "+")[0], "T")
	post.Createtime = dates[0] + " " + dates[1]
	post.Lastbacktime = timeStr
	if err = db.Model(&Post).Update(&post).Error; err != nil {
		logger.Error(err)
		c.JSON(http.StatusOK, gin.H{"err_code": constant.ErrorDB, "err_msg": constant.TranslateErrCode(constant.ErrorDB)})
		return
	}
	c.JSON(http.StatusOK, gin.H{"status": "success"})
}

//根据postid查询回复
func BackList(c *gin.Context) {
	type param struct {
		Id     int `json:"id" form:"id"`         //postId
		Userid int `json:"userid" form:"userid"` //userid
	}
	var p param
	var err error
	var Back []*models.Backpost
	if err = c.Bind(&p); err != nil {
		logger.Error(err)
		c.JSON(http.StatusOK, gin.H{"err_code": constant.ErrorParamWrong, "err_msg": constant.TranslateErrCode(constant.ErrorParamWrong)})
		return
	}
	db := c.MustGet("ty").(*gorm.DB)
	if err = db.Where("post_id = ?", p.Id).Find(&Back).Error; err != nil {
		logger.Error(err)
		c.JSON(http.StatusOK, gin.H{"err_code": constant.ErrorDB, "err_msg": constant.TranslateErrCode(constant.ErrorDB)})
		return
	}
	type list struct {
		Back     models.Backpost `json:"back"`
		UserName string          `json:"user_name"`
		Image    string          `json:"image"`
		IsZan    int          `json:"is_zan"`
	}
	var List []list
	for _, back := range Back {
		var l list
		var user models.User
		l.Back = *back
		if err = db.Where("id = ?", back.UserId).Find(&user).Error; err != nil {
			logger.Error(err)
			c.JSON(http.StatusOK, gin.H{"err_code": constant.ErrorDB, "err_msg": constant.TranslateErrCode(constant.ErrorDB)})
			return
		}
		l.UserName = user.Name
		l.Image = user.Image

		if err = db.Where("id = ?", back.UserId).Find(&user).Error; err != nil {
			logger.Error(err)
			c.JSON(http.StatusOK, gin.H{"err_code": constant.ErrorDB, "err_msg": constant.TranslateErrCode(constant.ErrorDB)})
			return
		}
		var coll []*models.Zan
		if err = db.Where("user_id = ? and post_id = ? and type = 1", p.Userid, back.Id).Find(&coll).Error; err != nil {
			logger.Error(err)
			c.JSON(http.StatusOK, gin.H{"err_code": constant.ErrorDB, "err_msg": constant.TranslateErrCode(constant.ErrorDB)})
			return
		}
		if len(coll) > 0 {
			l.IsZan = 1
		}else{
			l.IsZan = 0
		}
		fmt.Println("---------------------------------------------------------------")
		fmt.Println(l.IsZan)
		List = append(List, l)
	}
	c.JSON(http.StatusOK, gin.H{"list": &List})
}
